#!/usr/bin/perl
use Benchmark qw(:all);
use FindBin qw($Bin);
use Data::Dumper;
BEGIN {
	# Set our common library path.
	unshift @INC, $Bin.'/../lib';
}
# Get the run start time.
my $t0 = Benchmark->new;
# Cpan common library
use strict;
use warnings;
use utf8;
use Encode;
use HTML::LinkExtor;
use HTML::TreeBuilder::XPath;
use LWP::Simple;
use Digest::MD5 qw(md5_hex);

# Our common library
use Module::Module;
use File::Data;
use Date::Time;
use Feed::XML;
use Config::Config;
use Log::Write;
use Help::Message;

# Initialize run environment
# Global pram.
our $basepath      = "$Bin/..";
our $programname   = '';
our $programstatus = 0;

# Default execute parameters.
our %EXEC = (
			  'n' => 0,    # Sitename.
			  'l' => 0,    # Print site list.
			  'h' => 0,    # Print help information.
			  'v' => 0,    # Print version.
			  'i' => 1,    # Execute initialize function.
			  'f' => 1,    # Execute filter function.
			  'p' => 1,    # Execute parser function.
			  'd' => 1,    # Execute download images function.
			  'r' => 1,    # Execute remove stale images function.
			  'c' => 1,    # Execute check feed function.
			  'e' => 1,    # Execute send report email function.
			  'b' => 1
);               # Execute build tar function.

foreach my $param (@ARGV) {
	if ( $param =~ /^-([^-])([^-]*?)$/ ) {
		$EXEC{$1} = $2;
		$EXEC{$1} = 1 if ( $1 eq 'h' );
		$EXEC{$1} = 1 if ( $1 eq 'l' );
		$EXEC{$1} = 1 if ( $1 eq 'v' );
	} else {
		$programname = $param;
	}
}
if ( $EXEC{l} ) {
	print Help::Message::getModuleList();
	$EXEC{e} = 0;
	exit;
} elsif ( $EXEC{v} ) {
	print Help::Message::getVersion();
	$EXEC{e} = 0;
	exit;
} elsif ( $EXEC{h} || scalar(@ARGV) == 0 || $programname eq '' ) {
	print Help::Message::getHelp();
	$EXEC{e} = 0;
	exit;
} elsif ( !Help::Message::checkModuleExist($programname) ) {
	print "site is not exist!\n";
	$EXEC{e} = 0;
	exit;
}

# Load config file according to program name.
my $conf = new Config::Config;
our $memory   = {};
our $config   = $conf->getConfig( $Bin . '/../conf', $programname );
our $loginfo  = $conf->getIni( $config->{'log_info_file'} );
our $filedata = new File::Data();

# Set program name to config hash.
$config->{'programname'} = $programname;
$config->{'lockfile'}    = $config->{'base_path'} . "/data/$programname/lock";

# Set debug mode.
$config->{'mode'} = 'debug' if ( -e $config->{'base_path'} . '/debug' );

# Default package list.
my @requirefile =
  ( $config->{'base_path'} . '/module/' . $programname . '/parser.pm' );

# Require default package.
foreach my $filedir (@requirefile) {
	if ( -e $filedir ) {
		require($filedir);
	}
}

# Create a new object of module class.
my $module = new { 'module::' . $programname . '::parser' };
$module->{'config'} = $config;

# Run start
# A000000 : Crawler process starts runing,and process id is recorded.
Log::Write::setLog( 'A000000', { 'PID' => $$ } );

# Start up the module.
#$module->boot();

eval {
	print
	  "##################################################################\n";

	# Initialize crawler.
	#	$module->init() if ( $EXEC{i} );

	# Parser detail page ,get product infomation.
	my %notBrand = (
					 'wood'      => 'wood',
					 'QUOVADIS'  => 'QUOVADIS',
					 'RHODIA'    => 'RHODIA',
					 'Original'  => 'Original',
					 'mini'      => 'mini',
					 'YOGA'      => 'YOGA',
					 'Carry'     => 'Carry',
					 'sol'       => 'sol',
					 'omnioutil' => 'omnioutil',
					 'bucket'    => 'bucket',
					 'BABY'      => 'BABY',
					 'ESTAMPAS'  => 'ESTAMPAS',
					 'Puppy'     => 'Puppy',
					 'PH'        => 'PH',
					 'ml'        => 'ml',
					 'TOP'       => 'TOP',
					 'kids'      => 'kids',
					 'TOWN'      => 'TOWN',
					 'TRAIN'     => 'TRAIN',
					 'JUC'       => 'JUC',
					 'SET'       => 'SET',
					 'DE'        => 'DE',
					 'PIECES'    => 'PIECES',
					 'PLATEAU'   => 'PLATEAU',
					 'REPAS'     => 'REPAS',
					 'ASSIETTE'  => 'ASSIETTE',
					 'TASSE'     => 'TASSE',
					 'RAVOIR'    => 'RAVOIR',
					 'TABLE'     => 'TABLE',
					 'BOL'       => 'BOL',
					 'ANSES'     => 'ANSES',
					 'OFF'       => 'OFF',
					 'SSS'       => 'SSS',
					 'ALL'       => 'ALL',
					 'Bag'       => 'Bag',
					 'Box'       => 'Box',
					 'Tiny'      => 'Tiny',
					 'Umbrella'  => 'Umbrella',
					 'Way'       => 'Way',
					 'Pi-Bib'    => 'Pi-Bib',
					 'Bucket'    => 'Bucket'
	);		
	my $url =qq(<prod id="300573521" pre_order="no" web_offer="no" in_stock="yes" stock_quantity="1"><pId>9601554020121</pId><ean>5050122072428</ean><mpn>9601554020121</mpn><isbn>0000000000000</isbn><modelNumber>9601554020121</modelNumber><parentId>9601554</parentId><text><name>Notch neck jumper  - Grey</name><desc>This stylish, loose fit notch neck jumper features three button fastenings to the neck for a cool, laidback style!</desc></text><uri><awTrack>http://www.awin1.com/pclick.php?p=300573521&amp;a=79682&amp;m=1685</awTrack><awThumb>http://images.productserve.com/nothumb.gif</awThumb><awImage>http://images.productserve.com/noimage.gif</awImage><mLink>http://tracking.searchmarketing.com/click.asp?aid=1124097778</mLink><mImage>noimage.gif</mImage></uri><price curr="GBP"><buynow>22.40</buynow><store>0.00</store><rrp>0.00</rrp><delivery>3.50</delivery></price><cat><awCatId>179</awCatId><awCat>Men's Tops</awCat><mCat>Men&gt;Knitwear</mCat></cat><brand><brandName>M&amp;amp;Co</brandName></brand><valFrom>0000-00-00</valFrom><valTo>0000-00-00</valTo><comAmount>0.00</comAmount></prod>);
#	my $url ="http://www.neimanmarcus.com/product.jsp?itemId=prod77410074";
	my $hostUrl   = 'http://www.neimanmarcus.com/';
	#my $productID = 'ProductID';

	my %product = $module->parseAndgetInfo( $url, $hostUrl, \%notBrand )
	  if ( $EXEC{p} );
	print Dumper %product;
return;
};
if ($@) {

  # E999999 : Crawler exit,because unknow error,and the error cause is recorded.
	Log::Write::setLog( 'E999999', { 'ERRID' => $@ } );
} else {
	my $t1      = Benchmark->new;
	my $td      = timediff( $t1, $t0 );
	my $cputime = $td->[1] + $td->[2];

	# A999999 : Crawler process exits,and process id and CPU time is recorded.
	Log::Write::setLog( 'A999999', { 'CPU' => $cputime } );
}

# Delete process lock file.
unlink( $config->{'lockfile'} ) if ( -e $config->{'lockfile'} );

END {
	if ($?) {

  # E999999 : Crawler exit,because unknow error,and the error cause is recorded.
		Log::Write::setLog( 'E999999', { 'ERRID' => $? } );
	} else {

		# Send report mail to someone.
		# $module->sendMail() if($EXEC{e});
	}
}
sub changeFile{
	my $pro=shift;
	my $file="E:\\workspace\\jsp.txt";
	my $filename="E:\\workspace\\result.txt";
	my $content;
#	if(-e $file){
#		 open(RD,$file);
#		 while(my $line=<RD>){
#		 	$line= decode('utf8',$line); 
		 
		 	while($pro =~/;([^;]*);/isg){
		 		my $part =$1;
		 		if($part =~ /([^=]*)=([^=]*)/is){
			 		my $english =$1;
			 		my $china =$2;print $china;
			 		
			 		if(-e $file){
						 open(RD,$file);
						 while(my $line=<RD>){
						 $line= decode('utf8',$line); 
				 		if(index($line,$china,0) ne -1){
			 				$line =~s/$china/<fmt:message key="$english" \/>/isg;
	#						print $line;
							
							open(RW,'>>'.$filename);
#							binmode(RW, ":utf8");
							print RW "$line\n";
							close(RW);
							
							next;
			 			}
			 			else{
			 				open(RW,'>>'.$filename);
#							binmode(RW, ":utf8");
							print RW "$line\n";
							close(RW);
							next;
			 			}
		 				}
		 				close(RD);
		 		}
		 }
		 
		}
		
	return ;
}
sub readFile{
	my $filename=shift;
	my $content;
		if(-e $filename){
			 open(RD,$filename);
			 while(my $line=<RD>){
			 	$line= decode('utf8',$line); 
			 	$content .=";".$line;
			 	print $line;print "\n";
			 }
		}
	$content =$content.";";
	close(RD);	
	return $content;
}
1;
